/**
 * 
 */
package com.login.gwj.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.login.gwj.services.*;
import com.login.gwj.domain.*;

/**
 * @author Administrator
 * 
 */
public class receiptDaoImpl implements receiptDao {
	@Resource
	private SessionFactory sessionFactory;
	private Query query;
	public void addreceipt(receipt receipt){
		// TODO Auto-generated method stub
		org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
		session.save(receipt);
	}
	
	public int postAmount(int itemId) {
		int postAmount = 0;
		org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
		String hql = "select count(*) from receipt where itemId=?";
		query = session.createQuery(hql);
		query.setInteger(0, itemId);
		long  l = (Long)query.uniqueResult();
		postAmount = (int)l;
		return postAmount;
	}
	public int[] pageNumber(int postAmount, String pageNumberStr, int pageSize) {
		// TODO Auto-generated method stub
		int[] paging = new int[2];
		int pageNumber = 1;
		pageNumber = Integer.parseInt(pageNumberStr);
		if(pageNumber <= 0){
			pageNumber = 1;
		}
		int totalPage = 1;
		if(postAmount > 0){
			totalPage = postAmount%pageSize==0?(postAmount/pageSize):(postAmount/pageSize+1);
		}
		if(pageNumber > totalPage){
			pageNumber = totalPage;
		}
		paging[0] = pageNumber;
		paging[1] = totalPage;
		return paging;
	}
	public List<receipt> allreceipt(String string, int pageNumber, int pageSize) {
		// TODO Auto-generated method stub
		org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
		String hql = "from receipt where itemId=? order by postTime desc ";
		
		query=session.createQuery(hql);
		query.setFirstResult((pageNumber - 1)*pageSize);
		query.setMaxResults(pageSize);
		query.setString(0, string);
		List<receipt> receiptlist= query.list();
		return receiptlist;
	}

	public void deletereceipt(String receiptId) {
		// TODO Auto-generated method stub
		org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
		String hql = " from receipt where receiptId=?";
		query = session.createQuery(hql);
		query.setString(0, receiptId);
		List<receipt> receiptlist=query.list();
		session.delete(receiptlist.get(0));
	}
	

}
